﻿
using System;
using System.Collections.Generic;
using UnityEngine;

namespace ZyGame.Datable
{
    public interface IDatable : IDisposable
    {
        string name { get; }
    }

    internal class DatableManager : Singleton<DatableManager>
    {
        private Dictionary<string, IDatable> datables = new Dictionary<string, IDatable>();
        public T GetDatable<T>(string name) where T : IDatable
        {
            if (datables.TryGetValue(name, out IDatable datable))
            {
                return (T)datable;
            }
            datable = Activator.CreateInstance<T>();
            datables.Add(name, datable);
            return (T)datable;
        }

        public void Remove<T>(string name) where T : IDatable
        {
            datables.Remove(name);
        }

        internal void SetUserDatable(string cacheName, string datable)
        {
            PlayerPrefs.DeleteKey(cacheName);
            PlayerPrefs.SetString(cacheName, datable);
        }

        internal string GetUserDatable(string cacheName)
        {
            if (PlayerPrefs.HasKey(cacheName))
            {
                return PlayerPrefs.GetString(cacheName);
            }
            return string.Empty;
        }
    }

    
}